package com.sxt.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sxt.sys.domain.Role;
import com.sxt.sys.mapper.RoleMapper;
import com.sxt.sys.service.RoleService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.vo.RoleVo;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleMapper roleMapper;

	@Override
	public DataGridView queryAllRole(RoleVo roleVo) {
		Page<Object> page = PageHelper.startPage(roleVo.getPage(),
				roleVo.getRows());
		List<Role> list = this.roleMapper.queryAllRole(roleVo);
		System.out.println(page.getTotal());
		System.out.println(list.size());
		
	
		return new DataGridView(page.getTotal(), list);
	}

	@Override
	public void addRole(RoleVo roleVo) {
		// TODO Auto-generated method stub
		roleMapper.insertSelective(roleVo);
	}

	@Override
	public void updateRole(RoleVo roleVo) {
		// TODO Auto-generated method stub
		roleMapper.updateByPrimaryKeySelective(roleVo);
	}

	@Override
	public void deleteRole(RoleVo roleVo) {
		// TODO Auto-generated method stub
		roleMapper.deleteByPrimaryKey(roleVo.getRoleid());
	}

	@Override
	public void saveRoleMenus(RoleVo roleVo) {
		Integer roleid=roleVo.getRoleid();
        Integer [] menuIds=roleVo.getIds();
        //先清空数据库内用户原有的菜单数据，再跟新填入修改后拥有的菜单
        this.roleMapper.deleteRoleMenusByRoleId(roleid);
        //保存
        if(null!=menuIds&&menuIds.length>0){
        	for(Integer mid : menuIds){
        		this.roleMapper.saveRoleMenus(roleid,mid);
        	}
        }
	}


}
